package wangjg.xuexiban.activity.card;

import java.util.ArrayList;

import wangjg.xuexiban.R;
import wangjg.xuexiban.data.CardDAO;
import wangjg.xuexiban.entity.Card;
import wangjg.xuexiban.entity.CardName;
import wangjg.xuexiban.util.Constant;
import wangjg.xuexiban.widget.MyDialog;
import wangjg.xuexiban.widget.file.FileSelectorActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CardEditActivity extends BasicCardActivity {
	private View editView;
	private String edit_picPath;
	private String edit_voicePath;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.card_item_edit);
		editView = this.findViewById(R.id.LL);
		editView = this.getEditView();
		Long id = this.getCardId();
		if(id!=null){
			Card card = this.getCardDAO().get(id);
			this.edit_picPath = this.getCardFileFullPath(card.getPicture());
			if(card.getNameList().size()>0){
				this.edit_voicePath = this.getCardFileFullPath(card.getNameList().get(0).getSpeech());
				((TextView)editView.findViewById(R.id.name)).setText(card.getNameList().get(0).getName());
			}
			this.initView();
		}
	}

	protected String getCardFileFullPath(String filename){
		return null;
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
//		return super.onCreateOptionsMenu(menu);
		return false;
	}
	
	protected View getEditView(){
		final Button btn_set_title = (Button) editView.findViewById(R.id.btn_set_title);
		btn_set_title.setOnClickListener(new OnClickListener(){
			MyDialog.Callback callback = new MyDialog.Callback(){
				public void callback(String str) {
					TextView nameText = (TextView)editView.findViewById(R.id.name);
					nameText.setText(str);
				}
			};
			public void onClick(View v) {
				TextView nameText = (TextView)editView.findViewById(R.id.name);
				MyDialog.showInputDialog(CardEditActivity.this, "输入标题", nameText.getText().toString(), callback);
			}
		});
		
		//图片
		Button btn_set_pic = (Button) editView.findViewById(R.id.btn_set_pic);
		btn_set_pic.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent(CardEditActivity.this, FileSelectorActivity.class);
				intent.putExtra("path", edit_picPath);
				startActivityForResult(intent, 1);
			}
		});
		
		//声音
		Button btn_set_voice = (Button) editView.findViewById(R.id.btn_set_voice);
		btn_set_voice.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent(CardEditActivity.this, FileSelectorActivity.class);
				if(edit_voicePath!=null){
					intent.putExtra("path", edit_voicePath);
				}
				startActivityForResult(intent, 2);
			}
		});

		//保存
		Button btn_save_edit = (Button) editView.findViewById(R.id.btn_save_edit);
		btn_save_edit.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Toast toast=Toast.makeText(CardEditActivity.this, "已保存", Toast.LENGTH_SHORT);
				toast.show();
				
				TextView nameText = (TextView)editView.findViewById(R.id.name);
				String name = nameText.getText().toString();
				Card card = new Card();
				card.setId(getCardId());
				card.setPicture(edit_picPath);
				card.setNameList(new ArrayList<CardName>());
				card.getNameList().add(new CardName("english", name, edit_voicePath));
				getCardDAO().saveOrUpdate(card);
				CardEditActivity.this.setResult(1);
				finish();
			}
		});
		return editView;
	}

	private Long getCardId(){
		String s = getIntent().getStringExtra("id");
		if(s!=null){
			return Long.parseLong(s);
		}
		return null;
	}
	
    /*
	 * 选择文件
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		String filepath = null;
		Bundle bundle = null;
		if (data != null && (bundle = data.getExtras()) != null) {
			filepath = bundle.getString("path");
		}
		if(filepath==null){
			return ;
		}
    	if(requestCode==1){
    		this.edit_picPath = filepath;
    	}else if(requestCode==2){
    		this.edit_voicePath = filepath;
    	}
    	this.initView();
    }
	
	protected void initView(){
		TextView nameText = (TextView)editView.findViewById(R.id.name);
		String name = nameText.getText().toString();
		this.initItemViewDisplay(editView, edit_picPath, edit_voicePath, name);
	}
	
	protected void initItemViewDisplay(View itemView, String picpath, final String speechUrl, String name){
		
		if(name!=null){
			TextView tv = (TextView) itemView.findViewById(R.id.name);
			tv.setText(name);	
		}
		
		if(picpath!=null){
			ImageView iv = (ImageView) itemView.findViewById(R.id.imageView1);
			this.setImage(iv, picpath);
		}
		
		if(speechUrl!=null){
			final ImageView btn_play = (ImageView) itemView.findViewById(R.id.imageView_play);
			this.setAudio(btn_play, speechUrl);
		}

//		int screenWidth = this.getScreenWidth();
//		int screenHeight = this.getScreenHeight();
//		LinearLayout ll = (LinearLayout) itemView.findViewById(R.id.LL);
//		ll.setLayoutParams(new LinearLayout.LayoutParams(300, 400));//a
//		itemView.setLayoutParams(new Gallery.LayoutParams(screenWidth, screenHeight));//b (a/b二选一)
	}
	
	private CardDAO cardDAO;
	private CardDAO getCardDAO(){
		if(cardDAO==null){
			cardDAO = new CardDAO();
			
			SharedPreferences setting = this.getSharedPreferences(Constant.SHARED_PREFERENCES_NAME, 0);
			String root = setting.getString(Constant.URL_CARD_DOWN, Constant.DEFAULT_DATA_ROOT);
			String path = getIntent().getStringExtra("category_path");
			
			cardDAO.setCategoryPath(root+path);
		}
		return cardDAO;
	}
}

